<template>
  <div class="modifyManager common-container">
    <div class="common-top">
      <span class="left">
        <span class="go-back" @click.stop="$router.go(-1)">返回</span>
      </span>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
    <div class="common-bot">
      <div class="modify-bot">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item required label="名称" style="width:30%">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item required label="账号" style="width:30%">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item required label="密码" style="width:30%">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item required label="性别" style="width:30%">
            <el-select v-model="form.gender" placeholder="请选择">
              <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用">
            <el-switch v-model="form.enable"></el-switch>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import ManaApi from "@/api/";
export default {
  name: "ModifyManager",
  data() {
    return {
      form: {
        formId: 0,
        name: "",
        mobile: "",
        password: "",
        gender: "male",
        enable: true,
      },
      genderOptions: [
        {
          label: "男",
          value: "male",
        },
        {
          label: "女",
          value: "female",
        },
      ],
    };
  },
  methods: {
    getInfo() {
      ManaApi.manager
        .detail({
          targetId: this.$route.query.id,
        })
        .then((res) => {
          this.form = {
            formId: res.data.id,
            name: res.data.name,
            mobile: res.data.mobile,
            password: res.data.password,
            gender: res.data.gender,
            enable: res.data.enable,
          };
        });
    },
    checkInfo() {
      if (String(this.form.name).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写名称",
        });
        return false;
      }
      if (String(this.form.mobile).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写账号",
        });
        return false;
      }
      if (String(this.form.password).trim() == "") {
        this.$message({
          type: "error",
          message: "请填写密码",
        });
        return false;
      }
      return true;
    },
    save() {
      if (!this.checkInfo()) {
        return;
      }
      ManaApi.manager.modify(this.form).then((res) => {
        if (res.code == 200) {
          this.$router.go(-1);
        }
      });
    },
  },
  mounted() {
    if (this.$route.query.type == "edit") {
      this.getInfo();
    }
  },
};
</script>
<style scoped lang="scss">
.modifyManager {
  .modify-bot {
    width: 70%;
    padding: 30px 10px;
  }
}
</style>